package org.jeecg.modules.qyxx.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.qyxx.entity.BgFpHzNdWdxSxygysjxs;
import org.jeecg.modules.qyxx.entity.BgHzNdCwbb;
import org.jeecg.modules.qyxx.entity.BgHzNdSb;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.enumdemo.RedisKeyConstant;
import org.jeecg.modules.qyxx.mapper.BgHzNdSbMapper;
import org.jeecg.modules.qyxx.service.IBgHzNdSbService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateRange;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.jeecg.modules.qyxx.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 申报汇总数据
 * @Author: ZJJ
 * @Date: 2021-06-10
 * @Version: V1.0
 */
// @DS("multi-datasource1")申报
@Service
public class BgHzNdSbServiceImpl extends MppServiceImpl<BgHzNdSbMapper, BgHzNdSb> implements IBgHzNdSbService {
   /* @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;*/

    @Override
    public Object hzNdSb(BgNsrjbxxReq req) {
        DateUtils utils = DateUtils.getInstance();
        // String year = bgSjmkjgOrderService.getNewByDjxh(req.getDjxh());
        String year = utils.getYearToStr(req.getJssj());
        //LambdaQueryWrapper<BgHzNdSb> wrapper = Wrappers.lambdaQuery(BgHzNdSb.class);
        String djxh = req.getDjxh();
        List<String> tjnds = DateUtils.getInstance().nearlyThreeYears(req.getKssj(),req.getJssj());
        List<BgHzNdSb> list = baseMapper.hzNdSb(djxh, String.join(",", tjnds));
        for (BgHzNdSb one : list) {
            String tjnd = one.getTjnd();
            if(one.getYnse()!=null)
                one.setYnse(one.getYnse().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
            if(one.getSjteje()!=null)
                one.setSjteje(one.getSjteje().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
            if (year.equals(tjnd)) {
                DateRange range = utils.getLastQuarter();
                Date end = range.getEnd();
                String dyjd = tjnd + "03";
                String dejd = tjnd + "06";
                String dsjd = tjnd + "09";
                // String dsjd = tjnd + "12";
                if (dyjd.equals(utils.getMonthToStr(end))) {
                    one.setTjnd(tjnd + "年(第一季度)");
                } else if (dejd.equals(utils.getMonthToStr(end))) {
                    one.setTjnd(tjnd + "年(第二季度)");
                } else if (dsjd.equals(utils.getMonthToStr(end))) {
                    one.setTjnd(tjnd + "年(第三季度)");
                }else {
                    one.setTjnd(tjnd + "年");
                }
            } else {
                one.setTjnd(one.getTjnd() + "年");
            }
        }
        return list;
        //List<BgHzNdSb> list = new ArrayList<>();
        /*int i = 0;
        for (String tjnd : tjnds) {
            wrapper.clear();
            wrapper.eq(BgHzNdSb::getDjxh, djxh).eq(BgHzNdSb::getTjnd, tjnd);
            BgHzNdSb one = bgHzNdSbMapper.selectOne(wrapper);
            if (one != null) {
                if(one.getYnse()!=null)
                one.setYnse(one.getYnse().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
                if(one.getSjteje()!=null)
                one.setSjteje(one.getSjteje().divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
                if (year.equals(tjnd)) {
                    DateRange range = utils.getLastQuarter();
                    Date end = range.getEnd();
                    String dyjd = tjnd + "03";
                    String dejd = tjnd + "06";
                    String dsjd = tjnd + "09";
                    // String dsjd = tjnd + "12";
                    if (dyjd.equals(utils.getMonthToStr(end))) {
                        one.setTjnd(tjnd + "年(第一季度)");
                    } else if (dejd.equals(utils.getMonthToStr(end))) {
                        one.setTjnd(tjnd + "年(第二季度)");
                    } else if (dsjd.equals(utils.getMonthToStr(end))) {
                        one.setTjnd(tjnd + "年(第三季度)");
                    }
                } else {
                    one.setTjnd(one.getTjnd() + "年");
                }
                list.add(one);
                i++;
                if (i >= 3) {
                    break;
                }
            }
        }*/
    }
}
